﻿using System.Collections.Generic;
using anael.Data.BO;
using System;

namespace anael.Data.Model
{
    public partial class PersonneSet
    {
        public static List<EntriesTarget> NormalizeListOfPersonne(List<PersonneSet> listPerson)
        {
            List<EntriesTarget> list =  new List<EntriesTarget>();

            foreach (PersonneSet pers in listPerson)
            {
                EntriesTarget entriesTarget = new EntriesTarget();

                double[] entries = new double[22];

                Array.Copy(parseIntIntoBinary(pers.nbrPhoto, 6), 0, entries, 0, 6);
                Array.Copy(parseIntIntoBinary(pers.nbrContact, 8), 0, entries, 6, 8);
                Array.Copy(parseIntIntoBinary(pers.nbrGroupe, 6), 0, entries, 14, 6);
                Array.Copy(parseIntIntoBinary(pers.sexe, 1), 0, entries, 20, 1);
                Array.Copy(parseIntIntoBinary(pers.status, 1), 0, entries, 21, 1);
               
                entriesTarget.entries = entries;

                switch (pers.CategorieSet.nom)
                {
                    case "+++":
                        entriesTarget.target = new double[7] { 1, 0, 0, 0, 0, 0, 0 };
                        break;
                    case "++":
                        entriesTarget.target = new double[7] { 0, 1, 0, 0, 0, 0, 0 };
                        break;
                    case "+":
                        entriesTarget.target = new double[7] { 0, 0, 1, 0, 0, 0, 0 };
                        break;
                    case "0":
                        entriesTarget.target = new double[7] { 0, 0, 0, 1, 0, 0, 0 };
                        break;
                    case "-":
                        entriesTarget.target = new double[7] { 0, 0, 0, 0, 1, 0, 0 };
                        break;
                    case "--":
                        entriesTarget.target = new double[7] { 0, 0, 0, 0, 0, 1, 0 };
                        break;
                    case "---":
                        entriesTarget.target = new double[7] { 0, 0, 0, 0, 0, 0, 1 };
                        break;

                    default:
                        break;
                }

                list.Add(entriesTarget);
            }

            return list;
        }

        public static double[] NormalizePersonne(PersonneSet pers)
        {         
                EntriesTarget entriesTarget = new EntriesTarget();

                double[] entries = new double[22];

                Array.Copy(parseIntIntoBinary(pers.nbrPhoto, 6), 0, entries, 0, 6);
                Array.Copy(parseIntIntoBinary(pers.nbrContact, 8), 0, entries, 6, 8);
                Array.Copy(parseIntIntoBinary(pers.nbrGroupe, 6), 0, entries, 14, 6);
                Array.Copy(parseIntIntoBinary(pers.sexe, 1), 0, entries, 20, 1);
                Array.Copy(parseIntIntoBinary(pers.status, 1), 0, entries, 21, 1);

                return entries;
        }

        private static int[] parseIntIntoBinary(int integer, int size)
        {
            string strBin = Convert.ToString(integer, 2);

            if (size > strBin.Length)
            { 
            string nullBits = String.Empty;

            for (int i = 0; i < size - strBin.Length; i++)
            {
                nullBits += "0";
            }
                strBin = nullBits + strBin;
            }

            int[] bin = new int[size];

            for (int i = 0; i < size; i++)
            {
                if(strBin[i] == 49)
                bin[i] = 1;
                else
                bin[i] = 0;
            }

            return bin;
        }

    }
}